package main

import (
	"fmt"
	"sync"
)

//下面的迭代会有什么问题？

type threadSafeSet struct {
	sync.RWMutex
	s []interface{}
}

func (set *threadSafeSet) Iter() <-chan interface{} {
	ch := make(chan interface{}, len(set.s))
	go func() {
		set.RLock()
		defer set.RUnlock()

		for _, elem := range set.s {
			fmt.Printf("chan push|elem=%v\n", elem)
			ch <- elem
		}

		close(ch)
	}()
	return ch
}

func main() {

	th := threadSafeSet{
		s: []interface{}{"1", "2"},
	}

	ch := th.Iter()
	// 使用迭代器遍历集合
	for elem := range ch {
		fmt.Printf("chan pop|elem=%v\n", elem)
	}

}
